package DataStreamApi.Sink输出;


import org.apache.flink.api.common.serialization.SimpleStringEncoder;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.MemorySize;
import org.apache.flink.connector.file.sink.FileSink;
import org.apache.flink.core.execution.CheckpointingMode;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.filesystem.OutputFileConfig;
import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.DefaultRollingPolicy;

import java.time.Duration;
import java.util.Arrays;
import java.util.List;

/**
 * 从集合中读取数据
 */
public class Flink01_FILE {
    public static void main(String[] args) throws Exception {

        /**
         * 创建执行环境
         * IDEA运行的时候，
         * 需要导入依赖
         */
        StreamExecutionEnvironment executionEnvironment = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());


        executionEnvironment.setParallelism(1);
        /**
         * 必须开启Checkpointing，否则文件不可读。
         */
        executionEnvironment.enableCheckpointing(2000, CheckpointingMode.EXACTLY_ONCE);

        List<Integer> data = Arrays.asList(1, 2, 3, 4, 5);

        DataStreamSource<Integer> integerDataStreamSource1 = executionEnvironment.fromCollection(data);


        /**
         * 直接遍历元素
         */
        DataStreamSource<Integer> integerDataStreamSource3 = executionEnvironment.fromElements(1, 2, 3, 4, 5, 6, 7);


        FileSink flink01_file = FileSink.forRowFormat(new Path("data/output"), new SimpleStringEncoder("UTF-8"))
                .withOutputFileConfig(new OutputFileConfig("Flink01_FILE", ".txt"))
                .build();



        integerDataStreamSource3.sinkTo(flink01_file);


        executionEnvironment.execute();


    }
}
